package com.example.studentmanagersystem;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.fs.adapters.GuideViewPagerAdapter;
import com.fs.application.MyApplication;
import com.fs.fragment.GuideFragment1;
import com.fs.fragment.GuideFragment2;
import com.fs.fragment.GuideFragment3;
import com.fs.fragment.GuideFragment4;

public class GuidePageActivity extends FragmentActivity {

	private static final String PACKAGE_NAME="com.example.studentmanagersystem";
	private static final String VERSION_KEY="version_key";
	
	
	private ViewPager guideViewPager;
	private RadioGroup guideRadioGroup;
	private GuideFragment1 mFragment1;
	private GuideFragment2 mFragment2;
	private GuideFragment3 mFragment3;
	private GuideFragment4 mFragment4;
	
	private GuideViewPagerAdapter guideViewPagerAdapter;
	
	private List<Fragment> mListFragment = new ArrayList<Fragment>();
	
	@Override
	protected void onCreate(Bundle arg0) {
		

		super.onCreate(arg0);
		
		if(isFirstTimeLunch()){
			
			this.setContentView(R.layout.guide_avtivity_page);
			this.initView();
			
		}else if(!isFirstTimeLunch()){
			
			Intent intent=new Intent(this, MainActivity.class);
			this.startActivity(intent);
			this.finish();
			
		}
		
	}
	
	
	/**
	 * initialize widget
	 */
	private void initView() {

		//ViewPager
		this.guideViewPager=(ViewPager) this.findViewById(R.id.guide_viewpager);
		//RadioGroup
		this.guideRadioGroup=(RadioGroup) this.findViewById(R.id.guide_page_radioGroup);

		//Fragment
		this.mFragment1=new GuideFragment1();
		this.mFragment2=new GuideFragment2();
		this.mFragment3=new GuideFragment3();
		this.mFragment4=new GuideFragment4();
		
		//add fragment
		mListFragment.add(mFragment1);
		mListFragment.add(mFragment2);
		mListFragment.add(mFragment3);
		mListFragment.add(mFragment4);
		
		this.guideViewPagerAdapter=new GuideViewPagerAdapter(getSupportFragmentManager(), mListFragment);
		
		this.guideViewPager.setAdapter(guideViewPagerAdapter);
		
		 this.guideViewPager.setOnPageChangeListener(new MyPagerChangeListener());
		
	}
	
	/**
	 * custom listener
	 * @author cwf
	 *
	 */
	public class MyPagerChangeListener implements OnPageChangeListener {

		public void onPageSelected(int position) {

		}

		public void onPageScrollStateChanged(int arg0) {

		}

		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {
			
			((RadioButton) guideRadioGroup.getChildAt(position)).setChecked(true);
		}

	}
	
	
	/**
	 * guide page,if the first time,will start the guide;
	 * @return
	 */
	private boolean isFirstTimeLunch() {
		
		boolean b=false;
	
		try {
			PackageInfo info = getPackageManager().getPackageInfo(PACKAGE_NAME, 0);
			int currentVersion = info.versionCode;  
			SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);  
			int lastVersion = prefs.getInt(VERSION_KEY, 0);  
			
			
			if (currentVersion > lastVersion) {  
			     //if the version bigger more than last, the program is the first time start;
			     b=true;
			     //edit the version,when next time start the program,that will not run;
			     prefs.edit().putInt(VERSION_KEY,currentVersion).commit();  
			}else{
				
				b=false;
			}
			
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		} 
		
		Log.i("MYLOG",""+b);
		return b;
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
